<?php
// +----------------------------------------------------------------------
// | Description: 用户组
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\logic\FieldGrantLogic;
use app\admin\model\Group as GroupModel;
use think\response\Json;

class Group extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function initialize()
    {
        $action = [
            'permission'=>[''],
            'allow'=>['index','enables','read']
        ];

        event('CheckAuth',$action);

        $request = $this->request;
        $a = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 角色列表
     * @author Mr.Li
     * @param
     * @return
     */
    public function index()
    {
        $groupModel = new GroupModel();
        $param = $this->param;
        $data = $groupModel->getDataList($param,$this->userInfo['shop_id'],$this->userInfo['type']);
        return resultArray(['data' => $data]);
    }

    /**
     * 角色详情
     * @param  GroupModel $groupModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-08-13 10:28
     */
    public function read(GroupModel $groupModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $groupModel->getDataInfo($this->param);
        if (!$data) {
            return resultArray(['error' => $groupModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 角色添加
     * @param  GroupModel $groupModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-08-03 14:42
     */
    public function save(GroupModel $groupModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $groupModel->addData($this->param,$this->userInfo['shop_id']);
        if (!$data) {
            return resultArray(['error' => $groupModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '添加角色成功']);
    }

    /**
     * 角色编辑菜单
     * @param  GroupModel $groupModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-08-03 15:49
     */
    public function update(GroupModel $groupModel): Json
    {
        $groupModel = new GroupModel();
        $param      = $this->param;
        $param['group_id'] = $param['id'];
        $param['user_id']  = $this->userInfo['id'];
        $data = $groupModel->saveData($param);
        if (!$data) {
            return resultArray(['error' => $groupModel->getError()]);
        }
        return resultArray(['data' => '编辑成功']);
    }

    /**
     * 角色删除
     * @return Json
     * @throws \think\Exception
     * @author Mr.Liu Wuchen
     * @create 2021-05-22 9:20
     */
    public function delete(): Json
    {
        $groupModel = new GroupModel();
        $param      = $this->param;
        $dataInfo   = $groupModel->getDataById($param['id']);
        if (!$dataInfo) {
            return resultArray(['error' => '参数错误']);
        }
        if ($dataInfo['types']) {
            return resultArray(['error' => '系统角色，不能删除']);
        }
        $data = $groupModel->delGroupById($param['id']);
        if (!$data) {
            return resultArray(['error' => $groupModel->getError()]);
        }

        return resultArray(['data' => '删除成功']);
    }

    /**
     * 角色启用、禁用
     * @author Mr.Li
     * @param
     * @return
     */
    public function enables()
    {
        $groupModel = model('Group');
        $param = $this->param;
        $dataInfo = $groupModel->getDataById($param['id']);
        if (!$dataInfo) {
            return resultArray(['error' => '参数错误']);
        }
        if ($dataInfo['types']) {
            return resultArray(['error' => '系统角色，不能删除']);
        }
        $data = $groupModel->enableDatas($param['id'], $param['status'], true);
        if (!$data) {
            return resultArray(['error' => $groupModel->getError()]);
        }
        return resultArray(['data' => '操作成功']);
    }

    /**
     * 角色分类列表
     * @author Mr.Li
     * @param
     * @return
     */
    public function typeList()
    {
        $groupModel =  new GroupModel();
        $param = $this->param;
        $data = $groupModel->getTypeList($param);
        return resultArray(['data' => $data]);
    }
}
